package com.kaiwukj.android.ufamily.mvp.ui.page.service.community;

import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.BaseSwipeBackActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.Constant;
import com.kaiwukj.android.ufamily.di.component.DaggerServiceComponent;
import com.kaiwukj.android.ufamily.di.module.ServiceModule;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.CommunityNewResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;
import com.kaiwukj.android.ufamily.mvp.ui.widget.SpaceItemDecoration;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterUrlKt.CommunityNewsUrl)
public class CommunityNewListActivity extends BaseSwipeBackActivity<CommunityNewsPresenter>
        implements ServiceContract.CommunityNewView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_list)
    RecyclerView rvList;

    private CommunityNewAdapter mListAdapter;

    @OnClick(R.id.container_left)
    void onBackClick() {
        onBackPressedSupport();
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        super.setupActivityComponent(appComponent);
        DaggerServiceComponent.builder()
                .appComponent(appComponent)
                .serviceModule(new ServiceModule(this))
                .build().inject(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_list_normal;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tvTitle.setText("社区资讯");
        rvList.setLayoutManager(new LinearLayoutManager(this));
        rvList.addItemDecoration(new SpaceItemDecoration(15));
        mListAdapter = new CommunityNewAdapter();
        rvList.setAdapter(mListAdapter);
        mListAdapter.setOnItemClickListener((adapter, view, position) -> {
            ARouter.getInstance().build(ARouterUrlKt.CommunityServiceWeb)
                    .withInt("articleId", (int) adapter.getItemId(position))
                    .withInt("type", CommunityServiceWebActivity.NEWS)
                    .withString("url", Constant.SERVICE_COMMUNITY_NEWS)
                    .navigation();
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mPresenter.fetchCommunityNews(getApp().getUserInfo().getLastCommunityId());
    }

    @Override
    public void onFetchResult(List<CommunityNewResult> results) {
        mListAdapter.setNewInstance(results);
    }
}
